package GameCore;

import java.io.*;
import java.net.*;

public class Server 
{
    private static int _port;
    private static ServerSocket acceptSocket;
    
    private static DataTransmission player1;
    private static DataTransmission player2;
    private static DataTransmission player3;
    private static DataTransmission player4;
    
    public static void Init(int port)
    {
        _port = port;
    }

    public static boolean SendAndRecieve() {
        while(player1.connectSocket.isConnected() && player2.connectSocket.isConnected()
                && player3.connectSocket.isConnected() && player4.connectSocket.isConnected())
        {
            String message1 = player1.RecieveMessage();
            String message2 = player2.RecieveMessage();
            String message3 = player3.RecieveMessage();
            String message4 = player4.RecieveMessage();

            if(message1 != null)
            {
                player2.SendMessage(message1);
                player3.SendMessage(message1);
                player4.SendMessage(message1);
            }

            if(message2 != null)
            {
                player1.SendMessage(message2);
                player3.SendMessage(message2);
                player4.SendMessage(message2);
            }
            if(message3 != null)
            {
                player1.SendMessage(message3);
                player2.SendMessage(message3);
                player4.SendMessage(message3);
            }
            if(message4 != null)
            {
                player1.SendMessage(message4);
                player2.SendMessage(message4);
                player3.SendMessage(message4);
            }      
        }
        return false;
    }
    
    public static boolean Start()
    {
        try
        {
            acceptSocket = new ServerSocket(_port);
            return true;
        }
        catch(IOException io)
        {
            return false;
        }
    }
    
    public static boolean Stop()
    {
        try
        {
            acceptSocket.close();
            return true;
        }
        catch(IOException io)
        {
            return false;
        }
    }
    
    public static DataTransmission AcceptClient()
    {
        try
        {
            DataTransmission trans = new DataTransmission();
            trans.connectSocket = acceptSocket.accept();
            return trans;
        }
        catch(IOException io)
        {
            return null;
        }
    }
    
    public static void main(String args[])
    {
        Init(1430);
        if(Start() == true)
        {
            while(true)
            {
                player1 = AcceptClient();
                player1.ID = 1;
                
                player2 = AcceptClient();
                player2.ID = 2;
                
                player3 = AcceptClient();
                player3.ID = 3;
                
                player4 = AcceptClient();
                player4.ID = 4;
                break;
            }
        }
        if(!SendAndRecieve())
        {
            // khi có một kết nối bị ngắt thí xữ lí ở chổ này
        }
    }
}
